﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using DataAccessor.Protector;

namespace DataAccessor.Tools
{
    public partial class FormPrimary
    {
        private void OpenConfigurationSettingForm()
        {
            var database = treeHolistic.SelectedNode.Tag as HolisticDatabase;
            FormConfiguration form = new FormConfiguration(database);
            form.ConfigurationCollectionChanged += new ConfigurationCollectionChangedEventHandler(form_ConfigCollectionChanged);
            form.ConfigurationSettingChanged += new ConfigurationSettingChangedEventHandler(form_ConfigurationSettingChanged);
            LoadSettingForm(form);
        }

        void form_ConfigurationSettingChanged(UCConfiguration sender)
        {
            var node = treeHolistic.SelectedNode.Nodes[sender.Config.GetHashCode().ToString()];
            node.Text = sender.Config.Name;
        }

        void form_ConfigCollectionChanged()
        {
            var configNodes = treeHolistic.SelectedNode.Nodes;
            var database = treeHolistic.SelectedNode.Tag as HolisticDatabase;
            var waittingRemoveNodes = new List<TreeNode>();
            foreach (TreeNode configNode in configNodes)
            {
                if (!database.Config.Any(config => config.GetHashCode().ToString().Equals(configNode.Name)))
                {
                    waittingRemoveNodes.Add(configNode);
                }
            }
            foreach (var configNode in waittingRemoveNodes)
            {
                treeHolistic.SelectedNode.Nodes.Remove(configNode);
            }
            waittingRemoveNodes.Clear();
            waittingRemoveNodes = null;


            foreach (var config in database.Config)
            {
                var key = config.GetHashCode().ToString();
                if (!treeHolistic.SelectedNode.Nodes.ContainsKey(key))
                {
                    var node = treeHolistic.SelectedNode.Nodes.Add(key, config.Name);
                    node.Tag = config;
                }
            }
            CacheTSQL();
        }

        private void CacheTSQL()
        {
            _tsqlCache.Clear();
            foreach (var database in _holistic.Database)
            {
                IProtector protector = null;
                if (!string.IsNullOrEmpty(database.Protector))
                {
                    protector = HolisticParser.CreateProtector(database.Protector);
                }
                foreach (var config in database.Config)
                {
                    string key = string.Format("{0}.{1}", database.Name, config.Name);
                    if (!_tsqlCache.ContainsKey(config.GetHashCode()))
                    {
                        var filePath = Path.Combine(_holisticFileInfo.DirectoryName, config.FilePath);
                        if (!string.IsNullOrEmpty(database.Protector))
                        {
                            protector.DecryptTSQLFile(filePath, filePath);
                        }
                        _tsqlCache.Add(config.GetHashCode(), BuildTSQL(filePath));
                    }
                }
            }
        }

        private TSQL BuildTSQL(string filePath)
        {
            TSQL result = null;

            if (_holisticFileInfo == null)
            {
                result = CreateTSQL();
            }
            else
            {
                if (!File.Exists(filePath))
                {
                    result = CreateTSQL();
                }
                else
                {
                    result = new TSQLParser(filePath).TSQL;
                }
            }
            return result;
        }

        private TSQL CreateTSQL()
        {
            XmlDocument configDoc = new XmlDocument();
            configDoc.LoadXml(CONFIG_DOC_TEMPLATE);
            return new TSQLParser(configDoc).TSQL;
        }
    }
}
